unit BaseBll;

interface

uses System.Generics.Collections, BaseModel, BaseDAO, System.SysUtils,
  Connection;

type
  TSingleBll<T: TBaseModel> = class abstract
  private
  protected

    dao: TBaseDAO<T>;
    procedure BeforeDelete(id: integer); virtual; abstract;
    procedure CheckInputData(data: T); virtual; abstract;
  public
    constructor Create; virtual;
    destructor Destroy; override;

    function save(data: T): integer; virtual;
    function update(data: T): integer; virtual;
    function deleteById(id: integer): integer; virtual;
    procedure getAll(list: TObjectList<T>); virtual;

    procedure getByID(id: integer; data: T); virtual;
  end;


implementation

uses System.Rtti;

{ TSingleBll<T> }

constructor TSingleBll<T>.Create;
begin

end;

function TSingleBll<T>.deleteById(id: integer): integer;
begin
  BeforeDelete(id);
  dao.deleteById(id);
end;

destructor TSingleBll<T>.Destroy;
begin
//  if assigned(dao) then
//    freeandnil(dao);
  inherited;
end;

procedure TSingleBll<T>.getAll(list: TObjectList<T>);
begin
  dao.findAll(list);
end;

procedure TSingleBll<T>.getByID(id: integer; data: T);
begin
  dao.findById(id, data);
end;

function TSingleBll<T>.save(data: T): integer;
begin
  CheckInputData(data);
  result := dao.save(data);
end;

function TSingleBll<T>.update(data: T): integer;
begin
  CheckInputData(data);
  result := dao.update(data);
end;

{ TMastDetailBll<TM, TD> }

end.
